/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cc.altius.dao.impl;

import cc.altius.dao.SpecialistTypeDao;
import cc.altius.exception.CouldNotSaveToDataBaseException;
import cc.altius.model.SpecialistType;
import cc.altius.utils.DateUtils;
import java.util.Date;
import java.util.List;
import javax.sql.DataSource;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author sagar
 */
@Repository("SpecialistTypeDao")
public class SpecialistTypeDaoImpl implements SpecialistTypeDao {
    
    private SessionFactory sessionFactory;
    private HibernateTemplate hibernateTemplate;
    @Autowired
    private DataSource dataSource;

    @Autowired
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
        this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }

    @Override
    @Transactional
    public int addSpecialistType(SpecialistType specialistType) {
        return (Integer) this.hibernateTemplate.save(specialistType);
    }

    @Override
    @Transactional
    public int updateSpecialistType(SpecialistType specialistType, int specialistTypeId) {
       Query query;
        Session session = this.sessionFactory.getCurrentSession();
        Date date = DateUtils.getCurrentDateObject(DateUtils.IST);
        try {
            String hql = "Update "
                    + " SpecialistType as sptype"
                    + " set "
                    + " sptype.specialistTypeCode=:specialistTypeCode,"
                    + " sptype.specialistType=:specialistType"
                    + " WHERE sptype.specialistTypeId=:specialistTypeId";
            query = session.createQuery(hql);
            query.setParameter("specialistTypeCode", specialistType.getSpecialistTypeCode());
            query.setParameter("specialistType", specialistType.getSpecialistType());
            query.setParameter("specialistTypeId", specialistType.getSpecialistTypeId());
            int rowCount = query.executeUpdate();
            return rowCount;
        } catch (Exception e) {
            throw new CouldNotSaveToDataBaseException("Failed to add Specialist Type because" + e.getMessage());
        }
    }

    @Override
    @Transactional
    public List<SpecialistType> getSpecialistTypeList() {
       List<SpecialistType> specialistType = this.hibernateTemplate.find("from SpecialistType");
        return specialistType;
    }

    @Override
    @Transactional
    public SpecialistType getSpecialistTypeById(int specialistTypeId) {
        Session session = this.sessionFactory.getCurrentSession();
        Query query = session.createQuery("from SpecialistType where specialistTypeId=" + specialistTypeId);
        return (SpecialistType) query.uniqueResult();
    }
    
}
